//
//  MSPageControl.m
//  PeopleForTheFirstMall
//
//  Created by iOS on 2019/10/16.
//  Copyright © 2019 MS. All rights reserved.
//

#import "MSPageControl.h"

@implementation MSPageControl

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 去掉系统自带样式
        self.pageIndicatorTintColor = UIColor.clearColor;
        self.currentPageIndicatorTintColor = UIColor.clearColor;
        
        _activeImage = [UIImage imageNamed:@"currentPageImage"];
        _inactiveImage = [UIImage imageNamed:@"pageImage"];
        _activeImageSize = CGSizeMake(15, 6);
        _inactiveImageSize = CGSizeMake(15, 6);
    }
    return self;
}

- (void)setCurrentPage:(NSInteger)currentPage
{
    [super setCurrentPage:currentPage];
    [self setNeedsLayout];
    [self layoutIfNeeded];
    if (@available(iOS 14.0, *))
    {
        [self setPageControlImage];
    }
    else
    {
        [self updateDots];
    }
}

- (void)updateDots
{
    for (int i = 0; i < self.subviews.count; i++)
    {
        UIImageView *dot = [self imageViewForSubView:[self.subviews objectAtIndex:i]
                                                page:i];
        if (i == self.currentPage)
        {
            dot.image = self.activeImage;
            dot.frame = CGRectMake(dot.frame.origin.x,
                                   dot.frame.origin.y,
                                   self.activeImageSize.width,
                                   self.activeImageSize.height);
        }
        else
        {
            dot.image = self.inactiveImage;
            dot.frame = CGRectMake(dot.frame.origin.x,
                                   dot.frame.origin.y,
                                   self.inactiveImageSize.width,
                                   self.inactiveImageSize.height);
        }
    }
}

- (UIImageView *)imageViewForSubView:(UIView *)view
                                page:(int)page
{
    UIImageView *dot = nil;
    if ([view isKindOfClass:[UIView class]])
    {
        for (UIView *subView in view.subviews)
        {
            if ([subView isKindOfClass:[UIImageView class]])
            {
                dot = (UIImageView *)subView;
                break;
            }
        }
        
        if (dot == nil)
        {
            dot = [[UIImageView alloc] initWithFrame:view.bounds];
            [view addSubview:dot];
        }
    }
    else
    {
        dot = (UIImageView *)view;
    }
    
    return dot;
}

- (void)setPageControlImage
{
    UIView *fview1 = self;
    UIView *subView1 = fview1.subviews.firstObject;
    fview1 = subView1;
    subView1 = fview1.subviews.firstObject;
    fview1 = subView1;
    subView1 = fview1.subviews.firstObject;
    
    if (subView1 && [subView1 isKindOfClass:[UIImageView class]])
    {
        [self modfinPageControlSubViews:fview1];
    }
}

- (void)modfinPageControlSubViews:(UIView *)fView
{
    for (int i = 0; i < [fView.subviews count]; i++)
    {
        UIImageView *dot = [fView.subviews objectAtIndex:i];
        [dot.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        dot.backgroundColor = [UIColor clearColor];
        
        UIImageView *imageView = [UIImageView new];
        if (i == self.currentPage)
        {
            imageView.frame = CGRectMake(0, 0, self.activeImageSize.width, self.activeImageSize.height);
            imageView.image = self.activeImage;
            [dot addSubview:imageView];
            
        }
        else
        {
            imageView.frame = CGRectMake(0, 0, self.inactiveImageSize.width, self.inactiveImageSize.height);
            imageView.image = self.inactiveImage;
            [dot addSubview:imageView];
        }
    }
}

@end
